<?php
/**
 * clase no persistente donde el departamento recibe una tarea y crea los mecanismos para su aceptacion
 */
class PDepartmentAssignation {

	/**
	 * @type WFActivity
	 */
	public $workflow;
	/**
	 * @type MDepartamento
	 */
	public $department;
	/**
	 * @type WChronogram
	 */
	public $chronogram;
	/**
	 * @type FireableTransSet
	 */
	public $transSet;
	
	/**
	 * @type MUser
	 */
	public $user;
	
	function recover($workflow,$department) {
		$validate = FALSE;
		foreach($department->chronogramList as $chron) {
			if($chron->workflowRef->epgetObjectId() == $workflow->epgetObjectId()) {
				$this->workflow = $workflow;
				$this->department = $department;
				$this->chronogram = $chron;
				$validate = TRUE;
			}
		}
		return $validate;
	}
	
	function create($placeSet,$document) {
		$m = epManager :: instance();
		// select by document variant
		$variants = $document->workflowRef->networkRef->docTypeRef->variantList;
		$docVariant = $document->variant;
		if(count($variants) == 1){
			$dptoName = $placeSet->dptoVarietyList[0]->dptoName;
		}
		else{
			if($docVariant != NULL) {
				foreach($placeSet->dptoVarietyList as $dptoVariety) {
					foreach($dptoVariety->variantList as $variant) {
						if($docVariant->epGetObjectId() == $variant->epGetObjectId()) {
							$dptoName = $dptoVariety->dptoName;
						}
					}
				} 
			}
		}

		$query = "FROM MDepartamento where identificadorUnico = ?";
		$dptoList = $m->find($query, $dptoName);
		$this->department = $dptoList[0];
	}
	
	function possibleEmployee($transSet) {
		$m = epManager::instance();
		
		// search profiles

		$query = "FROM MPerfilUsuario as p where p.roleList.contains(r) and r = ?";
		$role = $transSet->roleRef;
		$userProfiles = $m->find($query,$role);
		
		$employeeList = array();
		foreach($userProfiles as $up) {
			$query = "From MEmpleado as e where e.loginSistema.perfilList.contains(?)";
			$empList2 = $m->find($query,$up);
			foreach($empList2 as $emp) {
				if(!in_array($emp,$employeeList) && $emp->departamento->epGetObjectId() == $this->department->epGetObjectId() ) $employeeList[] = $emp;
			}
		}
		return $employeeList;
	}
	
    function assignUser(& $employe, & $fTransSet){
 		$m = epManager::instance();
		$fireableTransSet = $this->workflow->fireableTransSet;
		
	
		$validTransSet = FALSE;
		foreach($fireableTransSet as $fTS) {
			if($fTransSet->epGetObjectId() == $fTS->epGetObjectId()){
				$validTransSet= TRUE;
				break;
			}
		}
		if(!$validTransSet) {
			return 1;
		}

		$validAssignation = FALSE;
		$query = "FROM MEmpleado as e where e.cargo.departamento = ?";
		$employes = $m->find($query, $this->department);
		foreach($employes as $empl) {
			if($employe->epgetObjectId() == $empl->epgetObjectId()) {
				$fTransSet->designedUser = $employe->loginSistema;
				$validAssignation = TRUE;
				break;
			}
		}
		if(!$validAssignation) {
			return 2;
		}
		$this->chronogram->userList[] = $employe->loginSistema; 
		return 0;
    }
    
    function receiveActivity(& $workflow, $fechaFinal) {
 	  	$m = epManager::instance();
 
		// Verificar el tipo de actividad y como debe ser ejecutada
		// Buscar si la actividad tiene en su marcacion actual al departamento
		$marking = $workflow->markingVector;
		$realMarking = explode('|',$marking);
		// seleccionar los places donde el departamento tiene ingerencia
		
		//Buscar las variantes del documento
		
		$document = $workflow->document;
		$variant = $document->variant;
		$network = $workflow->networkRef;
		foreach($this->department->chronogramList as $chron) {
			if($chron->workflowRef->epGetObjectId() == $workflow->epGetObjectId()) {
				echo "<br> ya tengo la actividad";
				return 1;
			}
		}

				
		// find the transSet corresponding to actual marking

				
		$chronogram = $m->create('WChronogram');
		$chronogram->reception = mktime();
		$chronogram->mustBePerformedBy = $fechaFinal;
		$chronogram->workflowRef = $workflow;
		$this->department->chronogramList[] = $chronogram;
		$chronogram->receivedTask = $this->department;
		
		$this->chronogram = $chronogram;
		$this->workflow = $workflow;
		return 0;
    }
    
    function deleteChronogram() {
  	  	$m = epManager::instance();
  	  	
  	  	$this->chronogram->delete();
    }
    
    function commitDept() {
    	$this->department->commit();
    	$this->chronogram->commit();
    	foreach($this->workflow->fireableTransSet as $fTS) {
    		$fTS->commit();
    	}
    }
}
?>